package com.lyc.service.impl;

import com.lyc.mapper.CategoryMapper;
import com.lyc.pojo.Category;
import com.lyc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    // 添加分类
    @Override
    public void addCategory(Category category) {
        // 设置默认值
        if (category.getStatus() == null) {
            category.setStatus("active");
        }
        if (category.getSortOrder() == null) {
            category.setSortOrder(0);
        }
        
        // 插入新分类
        categoryMapper.insert(category);
    }

    // 获取所有分类
    @Override
    public List<Category> getAllCategories() {
        return categoryMapper.getAllCategories();
    }

    // 更新分类
    @Override
    public void updateCategory(Category category) {
        categoryMapper.update(category);
    }

    // 删除分类
    @Override
    public void deleteCategory(Integer id) {
        // 删除分类
        categoryMapper.deleteById(id);
    }



    // 根据ID获取分类
    @Override
    public Category getCategoryById(Integer id) {
        return categoryMapper.findById(id);
    }
}